package com.alks.pad.service.serviceImpl.padCollectUser;

import com.alks.common.service.RedisService;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.common.utils.beanutils.ObjectUtils;
import com.alks.entity.data.entity.PcCollectUser2;
import com.alks.entity.data.enums.ResultCodeEnum;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.pad.commom.date.dto.PadUserTokenDto;
import com.alks.pad.commom.utils.JWTUtil;
import com.alks.pad.data.dto.padCollectUser.PadUserDeptDto;
import com.alks.pad.data.request.padCollectUser.PadCollectUserRequest;
import com.alks.pad.mapper.padCollectUser.PadUserMapper;
import com.alks.pad.service.padCollectUser.PadUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class PadUserServicelmpl implements PadUserService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private PadUserMapper padUserMapper;

    @Override
    public ResponseInfo login(PadCollectUserRequest request, HttpServletRequest servletRequest) {
        /*登录判空*/
        if (request == null) {
            return ResponseInfo.error(ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getMessage(), ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getCode());
        }
        if (BeanUtil.allIsEmpty(request.getPassword()) || BeanUtil.allIsEmpty(request.getLoginName())) {
            return ResponseInfo.error(ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getMessage(), ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getCode());
        }
        String username = request.getLoginName().toUpperCase();
        String companyId = request.getLoginCompanyId();

        /*根据账户名查询*/
        PcCollectUser2 baseSysUser = padUserMapper.getCollectUserAndPwd(null,username, null);
        //查不到此账号，账号不存在
        if (baseSysUser == null) {
            return ResponseInfo.error(ResultCodeEnum.LOGIN_USERNAME_NOT_EXIST.getMessage(), ResultCodeEnum.LOGIN_USERNAME_NOT_EXIST.getCode());
        }

        /*密码验证部分*/
        String loginName = request.getLoginName().toUpperCase(); //登录名
        String basePwd = baseSysUser.getCollectUserPwd(); //密码
        String collectType = baseSysUser.getCollectType(); //采集类型
        String deptType = baseSysUser.getDeptType(); //采集部门
        int appType = request.getAppType();
        //判断密码是否正确
        if (!basePwd.equals(request.getPassword())) {
            return ResponseInfo.error(ResultCodeEnum.LOGIN_PASSWORD_ERROR.getMessage(), ResultCodeEnum.LOGIN_PASSWORD_ERROR.getCode());
        }
        //判断当前账户能否登录当前app
        if(appType == 0){
            // 对于appType == 0，只有当deptType是"针车"时才允许登录
            if(!deptType.equals("针车")){
                return ResponseInfo.error(ResultCodeEnum.LOGIN_TYPE_IS_ERROR.getMessage(), ResultCodeEnum.LOGIN_TYPE_IS_ERROR.getCode());
            }
        }else if(appType == 1){
            if(deptType.equals("针车")){
                if(!collectType.equals("生产") && !collectType.equals("品质")){
                    return ResponseInfo.error(ResultCodeEnum.LOGIN_TYPE_IS_ERROR.getMessage(), ResultCodeEnum.LOGIN_TYPE_IS_ERROR.getCode());
                }
            }
        }else {
            return ResponseInfo.error(ResultCodeEnum.FRONT_IN_ERR.getMessage(), ResultCodeEnum.FRONT_IN_ERR.getCode());
        }

        /*把登录信息放入token*/
        PadUserTokenDto tokenDto = new PadUserTokenDto();
        BeanUtils.copyProperties(baseSysUser, tokenDto);
        //根据用户名id去查部门信息
        List<PadUserDeptDto> deptList = padUserMapper.getDeptByCollectUser(baseSysUser.getId());
        //拷贝部门信息
        if (tokenDto.getDepartmentList() == null) {
            tokenDto.setDepartmentList(new ArrayList<>());
        }
        List<PadUserDeptDto> list = tokenDto.getDepartmentList();
        if (ObjectUtils.isNotEmpty(deptList)) {
            list.addAll(deptList); // 使用addAll方法代替循环，简化代码
        }

        /*token*/
        String token = JWTUtil.generateToken(tokenDto);
        String key = loginName + "_" + "token";
        redisService.set(key, token);

        /*获取权限信息*/
        HashMap<String, Object> map = new HashMap<>();
        map.put("Authorization", key);
        map.put("name", baseSysUser.getCollectUserName());
        map.put("userInfo", tokenDto);
        return ResponseInfo.ok(map);
    }
}
